package com.well.menjin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.well.menjin.domain.Doorcontr;
import com.well.menjin.model.DoorcontrOpt;
import com.well.menjin.model.Sort;
import com.well.menjin.model.ZoneOpt;
import com.well.menjin.service.IDoorcontrService;
/**
 * 
* @ClassName: DoorcontrController 
* @author 姜俊健 
* @date 2016年6月27日 下午9:31:57 
*
 */
@Controller
@RequestMapping("/doorcontr")
public class DoorcontrController extends BaseController{
	@Autowired
	private IDoorcontrService doorcontrservice;
	
	/**
	 * 
	* @Title: listDoorcontr 
	* @Description: 列出所有控制器信息 
	* @return List<Doorcontr>    返回类型 
	* @throws
	 */
	@RequestMapping(value="/listDoorcontr",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String listDoorcontr(@RequestParam(value = "start", required=false) int start,
			@RequestParam(value="limit",required=false) int limit,
			@RequestParam(value="page",required=false) int page,
			@RequestParam(value="sort",required=false) String sorts,
			@RequestParam(value="controller_id",required=false) String controller_id,
			@RequestParam(value="controller_name",required=false) String controller_name,
			@RequestParam(value="ip_address",required=false) String ip_addreess){
		List<Sort> sorto = JSON.parseArray(sorts, Sort.class);
		List<Doorcontr> doorcontrs = doorcontrservice.getDoorcontrs(start,limit,page,sorto,controller_id,
				controller_name,ip_addreess);
		int totalCount =  doorcontrservice.getDoorcontrsCount(controller_id,controller_name,ip_addreess);
		return jsonBuilder.buildObjListToJson(Long.valueOf(totalCount), doorcontrs, true);

	}
	
	/**
	 * 
	* @Title: isExist 
	* @Description: 判断控制器是否存在 
	* @return void    返回类型 
	* @throws
	 */
	@RequestMapping(value="/isExist",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String isExist(Doorcontr doorcontr){
		boolean resulit = doorcontrservice.isExist(doorcontr.getController_id());
		if (resulit) {
			return jsonBuilder.returnSuccessJson("该控制器编号不重复");
		}else{
			return jsonBuilder.returnFailureJson("该控制器编号重复");
		}
	}
	
	/**
	 * 
	* @Title: addDoorcontr 
	* @Description: 添加控制器 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value="/addDoorcontr",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addDoorcontr(Doorcontr doorcontr){
		boolean result = doorcontrservice.addDoorcontr(doorcontr);
		if (result) {
			return jsonBuilder.returnSuccessJson("添加控制器成功");
		}else {
			return  jsonBuilder.returnFailureJson("添加控制器失败");
		}
	}
	
	/**
	 * 
	* @Title: updateDoorcontr 
	* @Description: 更新控制器信息
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value= "/updateDoorcontr",produces ="text/html; charset=UTF-8")
	@ResponseBody
	public String updateDoorcontr(Doorcontr doorcontr){
		boolean result = doorcontrservice.updateDoorcontr(doorcontr);
		if (result) {
			return jsonBuilder.returnSuccessJson("修改控制器成功");
		}else {
			return jsonBuilder.returnFailureJson("修改控制器失败");
		}
	}
	
	/**
	 * 
	* @Title: listDoorcontrOpts 
	* @Description: 列出用于在Combobox显示的信息
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value= "/listDoorcontrOpts",produces ="text/html; charset=UTF-8")
	@ResponseBody
	public String listDoorcontrOpts(){
		List<DoorcontrOpt> doorcontrOpts = doorcontrservice.getDoorcontrOpts();
		int totalCount = doorcontrservice.getDoorcontrsCount(null, null, null);
		return jsonBuilder.buildObjListToJson(Long.valueOf(totalCount), doorcontrOpts, true);
	}
}
